package ${t_package}.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import ${t_package}.domain.${t_bean};
import ${t_package}.service.${t_bean}Service;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
* @Title: Controller
* @Description: ${ftl_description}
* @author ${ftl_auth}
* @date ${ftl_create_time}
* @version V1.0
*
*/
@Controller
@RequestMapping("/${t_bean?uncap_first}")
public class ${t_bean}Controller {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private ${t_bean}Service ${t_bean?uncap_first}Service;


/**
*
* 分页
* @param dgm
* @param ${t_bean?uncap_first}
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, ${t_bean} ${t_bean?uncap_first}, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(${t_bean?uncap_first}Service.getPageList(${t_bean?uncap_first}, pr));
}

/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("${pageDir}/${t_bean?uncap_first}Form");
    ${t_bean} ${t_bean?uncap_first} = new ${t_bean}();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        ${t_bean?uncap_first} = ${t_bean?uncap_first}Service.findById(id);
    } else {

    }
    modelAndView.addObject("${t_bean?uncap_first}", ${t_bean?uncap_first});
    return modelAndView;
}


/**
* 保存信息
*
* @param ${t_bean?uncap_first}  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, ${t_bean} ${t_bean?uncap_first}) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(${t_bean?uncap_first}.getId())) {//更新
        ${t_bean} r = ${t_bean?uncap_first}Service.findById(${t_bean?uncap_first}.getId());
        MyBeanUtils.copyBeanNotNull2Bean(${t_bean?uncap_first}, r);
        ${t_bean?uncap_first} = r;
    }else {

    }
    ${t_bean?uncap_first}Service.save(${t_bean?uncap_first});
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    ${t_bean?uncap_first}Service.delete(id);
    j.setMsg("删除成功");
    return j;
}

}